package edu.pku.sei.transformation.editor.controller.commands.node;

import org.eclipse.gef.commands.Command;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.transformation.editor.model.CompositeTransformation;
import edu.pku.sei.transformation.editor.model.execution.ControlNode;
import edu.pku.sei.transformation.editor.model.execution.Model;
import edu.pku.sei.transformation.editor.model.execution.TransformationExecution;

public class CompositeNodeCreateCommand extends Command {
	private MetaModelElement node;
	private CompositeTransformation transformation;
	public CompositeNodeCreateCommand(MetaModelElement node, CompositeTransformation transformation) {
		this.node = node;
		this.transformation = transformation;
	}
	
	@Override
	public boolean canExecute() {
		if (node == null || transformation == null) {
			return false;
		} else
			return super.canExecute();
	}
	
	@Override
	public void execute() {
		node.setContainer(transformation);
		if (node instanceof TransformationExecution) {
			transformation.getSubExecutions().add((TransformationExecution) node);
		} else if (node instanceof ControlNode) {
			transformation.getControlNodes().add((ControlNode) node);
		} else if (node instanceof Model) {
			transformation.getIntermediateModels().add((Model) node);
		}
	}
	
	@Override
	public void undo() {
		node.setContainer(null);
		if (node instanceof TransformationExecution) {
			transformation.getSubExecutions().remove((TransformationExecution) node);
		} else if (node instanceof ControlNode) {
			transformation.getControlNodes().remove((ControlNode) node);
		} else if (node instanceof Model) {
			transformation.getIntermediateModels().remove((Model) node);
		}
	}
}
